#include "common.h"


int RES_CreateResponse(Conn *c)
{
    c->response = DS_Pcalloc(c->pool, sizeof(Response));
    if (c->response == NULL) {
        Log_Write(g_server->log, "RESPONSE: no memory");
        return HTTP_ERR;
    }

    return HTTP_OK;
}

void SetHttpStatus(Conn *c, int status)
{
    c->response->status = status;
}

void SetContentLength(Conn *c, size_t length)
{
    c->response->content_length = length;
}

int SendHttpHeader(Conn *c)
{
    Buf *buf;
    int sendlen;

    buf = DS_CreateTempBuf(c->pool, 256);
    /*
    if (c->version == 1001) {
        memcpy(buf->last, "HTTP/1.1 ", 9);
        buf->last += 9;
    }
    else {
        memcpy(buf->last, "HTTP/1.0 ", 9);
        buf->last += 9;
    }
    */
    if (c->response->status == 200) {
        memcpy(buf->last, "200 OK\r\n", 8);
        buf->last += 8;
    }
    else if (c->response->status == 404) {
        memcpy(buf->last, "404 Not Found\r\n", 15);
        buf->last += 15;
    }
    else {
        memcpy(buf->last, "403 Forbidden\r\n", 15);
        buf->last += 15;
    }
    *buf->last++ = '\r';
    *buf->last++ = '\n';

    if (AppendResponseBuf(c,buf->pos,buf->last - buf->pos) != HTTP_OK) {
        return HTTP_ERR;
    }
    
    return HTTP_OK;
}
int AppendResponseBuf(Conn *c, char *buffer, int len)
{
    Chain *chain, *temp;
    Buf *buf;
    
    chain = DS_Pcalloc(c->pool, sizeof(Chain));
    if (chain == NULL) {
        return HTTP_ERR;
    }

    buf = DS_CreateTempBuf(c->pool, len);
    memcpy(buf->last, buffer, len);
    buf->last += len;
    chain->buf = buf;
    chain->next = NULL;
    
    if (c->response->chain == NULL) {
        c->response->chain = chain;
        return HTTP_OK;
    }

    temp = c->response->chain;
    while (temp->next) {
        temp = temp->next;
    }
    temp->next = chain;

    return HTTP_OK;
    
}

int AppendResponseFile(Conn *c, int fd, int offset)
{
    Chain *chain, *temp;
    Buf *buf;
    struct stat fs;
    
    chain = DS_Pcalloc(c->pool, sizeof(Chain));
    if (chain == NULL) {
        return HTTP_ERR;
    }

    buf = DS_Pcalloc(c->pool, sizeof(Buf));
    buf->in_file = 1;
    buf->fileDes = fd;
    buf->file_pos = 0;
    fstat(fd, &fs);
    buf->file_last = fs.st_size;
    
    chain->buf = buf;
    chain->next = NULL;
    
    if (c->response->chain == NULL) {
        c->response->chain = chain;
        return HTTP_OK;
    }

    temp = c->response->chain;
    while (temp->next) {
        temp = temp->next;
    }
    temp->next = chain;

    return HTTP_OK;
}

void SendHttpBody(Conn *c, int mode)
{
     Chain *chain;
     int rc;
     
     chain = c->response->chain;
     rc = OutputChain(c,chain);
     if (rc == NET_OK) {
        FinalizeConn(c);
     } else if (rc == NET_AGAIN) {
	    c->write->handler = WriteClientBuf; 
        EVENT_Handle_Write_Event(c->write, EVENT_CLEAR_EVENT);
     }	else {
	    FinalizeConn(c);
     }
}

